package com.hugoo.imp.buzz;

import com.hugoo.core.util.TL;
import com.hugoo.imp.buzz.mo.ColumnInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Lovelock.Chan
 */
public abstract class FileValidation extends FileInfo {

    public FileValidation(String sid, String scod) {
        super(sid, scod, null);
    }

    public String getInfo() {
        StringBuilder sb = new StringBuilder();
        sb.append("<dl>");
        if (info.size() > 1000) {
            int i = 0;
            for (String l : info) {
                sb.append("<dd>").append(l).append("</dd>");
                if (i++ > 1000) {
                    sb.append("<dd>无效记录太长,以下均不展示……</dd>");
                    break;
                }
            }
        } else {
            for (String l : info) {
                sb.append("<dd>").append(l).append("</dd>");
            }
        }
        sb.append("</dl>");
        return sb.toString();
    }

    private void _init_vl_info(StringBuilder sb, int r, int c, String key, String val) {
        sb.append("【第").append(r).append("行，第").append(c).append("列】").append(key).
                append("字段值【").append(val).append("】");
    }

    @Override
    protected void __buildSysColumn(List<ColumnInfo> col) {
    }

    /**
     * 定规则： MaL:30 指定最大值 MiL:0 指定最小值 PaT:《正则表达式》 指定验证格式
     *
     * @param val
     * @param rule
     * @param info
     * @return
     */
    protected boolean validation(int r, int c, ColumnInfo ci, String val, List<String> info) {
        if (ci.isVl()) {
            StringBuilder sb = null;
            if (TL.isEmpty(val)) {
                if (ci.isVlMin()) {
                    sb = new StringBuilder();
                    this._init_vl_info(sb, r, c, ci.getDesc(), val);
                    sb.append("不能为空！");
                }
            } else {
                if (ci.isVlMax() && val.length() > ci.getMaxLength()) {
                    sb = new StringBuilder();
                    this._init_vl_info(sb, r, c, ci.getDesc(), val);
                    sb.append("长度为【").append(val.length()).append("】，超过了设置的最大长度【").
                            append(ci.getMaxLength()).append("】");
                } else if (ci.isVlMin() && val.trim().length() < ci.getMinLength()) {
                    sb = new StringBuilder();
                    this._init_vl_info(sb, r, c, ci.getDesc(), val);
                    sb.append("长度为【").append(val.trim().length()).append("】，小于指定的最小长度【").
                            append(ci.getMinLength()).append("】");
                }
                if (ci.isVlPat() && !val.matches(ci.getRules())) {
                    if (sb == null) {
                        sb = new StringBuilder();
                        this._init_vl_info(sb, r, c, ci.getDesc(), val);
                        sb.append("不符合指定规则【").append(ci.getRules()).append("】!");
                    } else {
                        sb.append("，并且不符合指定规则【").append(ci.getRules()).append("】!");
                    }
                }
            }
            if (sb != null) {
                info.add(sb.toString());
                return false;
            }
        }
        return true;
    }

    public abstract void validation(String file, Map m);
    public final static String VALI_RUST = "VALI_RUST";
    public final static String VALI_REASON = "VALI_REASON";
    public final static String VALI_INFO = "VALI_INFO";
    protected final List<String> info = new ArrayList();
;
}
